package com.sunshine.wrongquestionset.activity.question_set

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.button.MaterialButton
import com.google.android.material.card.MaterialCardView
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.textview.MaterialTextView
import com.sunshine.wrongquestionset.R
import com.sunshine.wrongquestionset.callback.OnClickListener
import com.sunshine.wrongquestionset.callback.OnListEmptyListener
import com.sunshine.wrongquestionset.room.entity.AppConfigEntity
import com.sunshine.wrongquestionset.room.entity.QuestionSetActivityBean
import com.sunshine.wrongquestionset.room.entity.QuestionSetEntity
import com.sunshine.wrongquestionset.view.HasEmptyRecyclerAdapter

/**
 * @author sunshine
 * @date 2021/3/29
 */
class QuestionSetAdapter(
    private val list: ArrayList<QuestionSetActivityBean>?,
    private val appConfigEntity: AppConfigEntity?,
    private val viewModel: QuestionSetViewModel,
    private val owner: LifecycleOwner,
    private val callback: OnClickListener
): HasEmptyRecyclerAdapter<QuestionSetAdapter.ViewHolder>(list) {

    private lateinit var context: Context

    inner class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
        val root: MaterialCardView = itemView.findViewById(R.id.item_question_set_root)
        val myName: MaterialTextView = itemView.findViewById(R.id.question_set_name)
        val description: MaterialTextView = itemView.findViewById(R.id.description)
        val description1: MaterialTextView = itemView.findViewById(R.id.description_1)
        val subject: MaterialTextView = itemView.findViewById(R.id.textView_subject)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        context = parent.context
        return ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_question_set, parent, false))
    }

    override fun getItemCount(): Int {
        return list?.size ?: 0
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.myName.text = list!![position].name

        holder.subject.text = list[position].subjectName

        holder.description.text = list[position].description
        //暂时取消默认选项
        //if (list[position].id == appConfigEntity?.nowQuestionSetId) holder.default.text = "默认"
        viewModel.getQuestionCountInQuestionSet(list[position].questionSetId).observe(owner, Observer {
            holder.description1.text = "共${it}道题目"
        })

        holder.root.setOnClickListener {
            callback.onClick(position)
        }
        holder.root.setOnLongClickListener {
            callback.onLongClick(position)
            true
        }
    }

//    /**
//     * 添加列表数据变化监听器
//     */
//    fun addListEmptyListener(callback: OnListEmptyListener) {
//        registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() {
//            override fun onChanged() {
//                super.onChanged()
//                callback.isEmpty(list.isNullOrEmpty())
//            }
//        })
//    }
}